﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Globalization;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.Utils;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PR_CommandDtls : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private PromoteEmployeeBLL prBLL = new PromoteEmployeeBLL();
        private SalaryPromotionConfigBLL pcBLL = new SalaryPromotionConfigBLL();
        private AppraisalsBLL appraisalsBLL = new AppraisalsBLL();
        private SystemYearTypeBLL yearTypeBLL = new SystemYearTypeBLL();
        private ICommandDetailBLL dcmdBLL = new CommandDetailBLL();
        private ICommandBLL cmdBLL = new CommandBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblID.Text = Request.QueryString["id"];
                LoadDataGrid();
                BindLabel();
                ListYear();
            }
        }
        private void LoadDataGrid()
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();

            ds = dcmdBLL.LoadCommandDetails((int?)null, Convert.ToInt32(lblID.Text), (int?)null);
            dt = ds.Tables[0];

            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }
        private void BindLabel()
        {
            DataSet ds2 = new DataSet();
            DataTable dt2 = new DataTable();

            ds2 = cmdBLL.LoadCommands(SystemInfo._CmdEmployee, Convert.ToInt32(lblID.Text), (int?)null, (int?)null);
            dt2 = ds2.Tables[0];
            if (dt2.Rows.Count > 0)
            {
                lblCmdName.Text = dt2.Rows[0]["cmd_name"].ToString();
                lblRefNo.Text = dt2.Rows[0]["cmd_ref_no"].ToString();
                lblAppDate.Text = string.Format("{0:dd/MM/yyyy}", dt2.Rows[0]["approved_date"]);
            }

            DataSet ds = yearTypeBLL.GetYearType(int.Parse(Session["EMPTYPE"].ToString()));
            lblTypeYear.Text = ds.Tables[0].Rows[0]["year_type_name"].ToString();    
        }
        public void ListYear()
        {
            DataTable dt = new DataTable();
            DataColumn dc = dt.Columns.Add("Name");
            int myYearNow = DateTime.Today.Year + 543;

            for (int i = myYearNow - 10; i <= myYearNow + 10; i++)
            {
                dt.Rows.Add(Convert.ToString(i));
            }
            ddlYear.DataSource = dt;
            ddlYear.DataBind();
        }
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["cmd_dtls_seq"]);
                dcmdBLL.Delete(seq, ssMng.EmpSeqI);
            }
  
            LoadDataGrid();
        }

        protected void cmdAutoInsert_Click(object sender, EventArgs e)
        {
            int nPrmSeq = pcBLL.GetPromotionSequence(Convert.ToInt32(ddlYear.SelectedValue)
                            , Convert.ToInt32(radPromotion.SelectedValue)
                            , Convert.ToInt32(Session["EMPTYPE"].ToString()));
            if (nPrmSeq > 0)
            {
                prBLL.InsertPromotionCommands(Convert.ToInt32(lblID.Text), nPrmSeq);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "onclick", "MessageAlert('ไม่สามารถเพิ่มข้อมูลได้ เนื่องจากยังไม่มีข้อมูลการเลื่อนเงินเดือน')", true);
                return;
            }
            LoadDataGrid();
        }
    }
}
